{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e9e9f934",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用模块\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a4f7fea5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.查看api文档获取base url 目标文档\n",
    "\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    \n",
    "# 2. 查看API请求/调用 方法 POST\n",
    "\n",
    "# 3. 准备payload\n",
    "\n",
    "payload = {\n",
    "    'api_key':'s6i96sHO1im2fnWJK0lFOjWyOiJvT1cK',\n",
    "    'api_secret':'1_Rm3dTYcF7iH3LzLVhywDZKasjLXW70',\n",
    "    'image_url':'https://img.bagevent.com/resource/20190812/11324877515211.jpg?imageView2/2/w/480/interlace/1/q/100',\n",
    "    'return_landmark':1,\n",
    "    'return_attributes':'gender,age,smiling'\n",
    "}\n",
    "\n",
    "# 4. 发送请求\n",
    "\n",
    "r = requests.post(url=base_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a9e43448",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "48491f05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647913711,dcdc1029-80ce-4c66-9dd5-0d057557e7b1',\n",
       " 'time_used': 392,\n",
       " 'faces': [{'face_token': '5658fb9039b05189a808340fa1e24244',\n",
       "   'face_rectangle': {'top': 72, 'left': 51, 'width': 98, 'height': 98},\n",
       "   'landmark': {'contour_chin': {'x': 99, 'y': 168},\n",
       "    'contour_left1': {'x': 51, 'y': 93},\n",
       "    'contour_left2': {'x': 51, 'y': 104},\n",
       "    'contour_left3': {'x': 51, 'y': 115},\n",
       "    'contour_left4': {'x': 53, 'y': 126},\n",
       "    'contour_left5': {'x': 55, 'y': 136},\n",
       "    'contour_left6': {'x': 60, 'y': 146},\n",
       "    'contour_left7': {'x': 67, 'y': 155},\n",
       "    'contour_left8': {'x': 76, 'y': 161},\n",
       "    'contour_left9': {'x': 87, 'y': 166},\n",
       "    'contour_right1': {'x': 149, 'y': 92},\n",
       "    'contour_right2': {'x': 149, 'y': 103},\n",
       "    'contour_right3': {'x': 148, 'y': 114},\n",
       "    'contour_right4': {'x': 147, 'y': 126},\n",
       "    'contour_right5': {'x': 145, 'y': 137},\n",
       "    'contour_right6': {'x': 140, 'y': 147},\n",
       "    'contour_right7': {'x': 132, 'y': 155},\n",
       "    'contour_right8': {'x': 123, 'y': 162},\n",
       "    'contour_right9': {'x': 112, 'y': 167},\n",
       "    'left_eye_bottom': {'x': 77, 'y': 94},\n",
       "    'left_eye_center': {'x': 78, 'y': 91},\n",
       "    'left_eye_left_corner': {'x': 69, 'y': 91},\n",
       "    'left_eye_lower_left_quarter': {'x': 72, 'y': 93},\n",
       "    'left_eye_lower_right_quarter': {'x': 82, 'y': 94},\n",
       "    'left_eye_pupil': {'x': 77, 'y': 91},\n",
       "    'left_eye_right_corner': {'x': 87, 'y': 93},\n",
       "    'left_eye_top': {'x': 78, 'y': 87},\n",
       "    'left_eye_upper_left_quarter': {'x': 72, 'y': 88},\n",
       "    'left_eye_upper_right_quarter': {'x': 83, 'y': 89},\n",
       "    'left_eyebrow_left_corner': {'x': 61, 'y': 82},\n",
       "    'left_eyebrow_lower_left_quarter': {'x': 67, 'y': 80},\n",
       "    'left_eyebrow_lower_middle': {'x': 73, 'y': 80},\n",
       "    'left_eyebrow_lower_right_quarter': {'x': 80, 'y': 80},\n",
       "    'left_eyebrow_right_corner': {'x': 88, 'y': 79},\n",
       "    'left_eyebrow_upper_left_quarter': {'x': 65, 'y': 75},\n",
       "    'left_eyebrow_upper_middle': {'x': 73, 'y': 74},\n",
       "    'left_eyebrow_upper_right_quarter': {'x': 81, 'y': 75},\n",
       "    'mouth_left_corner': {'x': 83, 'y': 138},\n",
       "    'mouth_lower_lip_bottom': {'x': 99, 'y': 145},\n",
       "    'mouth_lower_lip_left_contour1': {'x': 91, 'y': 138},\n",
       "    'mouth_lower_lip_left_contour2': {'x': 87, 'y': 142},\n",
       "    'mouth_lower_lip_left_contour3': {'x': 92, 'y': 144},\n",
       "    'mouth_lower_lip_right_contour1': {'x': 107, 'y': 138},\n",
       "    'mouth_lower_lip_right_contour2': {'x': 112, 'y': 142},\n",
       "    'mouth_lower_lip_right_contour3': {'x': 106, 'y': 144},\n",
       "    'mouth_lower_lip_top': {'x': 99, 'y': 138},\n",
       "    'mouth_right_corner': {'x': 117, 'y': 138},\n",
       "    'mouth_upper_lip_bottom': {'x': 99, 'y': 138},\n",
       "    'mouth_upper_lip_left_contour1': {'x': 94, 'y': 133},\n",
       "    'mouth_upper_lip_left_contour2': {'x': 88, 'y': 135},\n",
       "    'mouth_upper_lip_left_contour3': {'x': 91, 'y': 138},\n",
       "    'mouth_upper_lip_right_contour1': {'x': 103, 'y': 133},\n",
       "    'mouth_upper_lip_right_contour2': {'x': 110, 'y': 135},\n",
       "    'mouth_upper_lip_right_contour3': {'x': 107, 'y': 138},\n",
       "    'mouth_upper_lip_top': {'x': 98, 'y': 134},\n",
       "    'nose_contour_left1': {'x': 91, 'y': 93},\n",
       "    'nose_contour_left2': {'x': 88, 'y': 111},\n",
       "    'nose_contour_left3': {'x': 91, 'y': 121},\n",
       "    'nose_contour_lower_middle': {'x': 99, 'y': 123},\n",
       "    'nose_contour_right1': {'x': 107, 'y': 93},\n",
       "    'nose_contour_right2': {'x': 110, 'y': 111},\n",
       "    'nose_contour_right3': {'x': 106, 'y': 121},\n",
       "    'nose_left': {'x': 84, 'y': 118},\n",
       "    'nose_right': {'x': 113, 'y': 118},\n",
       "    'nose_tip': {'x': 98, 'y': 114},\n",
       "    'right_eye_bottom': {'x': 122, 'y': 94},\n",
       "    'right_eye_center': {'x': 122, 'y': 92},\n",
       "    'right_eye_left_corner': {'x': 112, 'y': 93},\n",
       "    'right_eye_lower_left_quarter': {'x': 117, 'y': 94},\n",
       "    'right_eye_lower_right_quarter': {'x': 127, 'y': 93},\n",
       "    'right_eye_pupil': {'x': 122, 'y': 90},\n",
       "    'right_eye_right_corner': {'x': 131, 'y': 91},\n",
       "    'right_eye_top': {'x': 122, 'y': 87},\n",
       "    'right_eye_upper_left_quarter': {'x': 116, 'y': 89},\n",
       "    'right_eye_upper_right_quarter': {'x': 127, 'y': 88},\n",
       "    'right_eyebrow_left_corner': {'x': 111, 'y': 78},\n",
       "    'right_eyebrow_lower_left_quarter': {'x': 119, 'y': 79},\n",
       "    'right_eyebrow_lower_middle': {'x': 126, 'y': 79},\n",
       "    'right_eyebrow_lower_right_quarter': {'x': 134, 'y': 79},\n",
       "    'right_eyebrow_right_corner': {'x': 140, 'y': 81},\n",
       "    'right_eyebrow_upper_left_quarter': {'x': 118, 'y': 74},\n",
       "    'right_eyebrow_upper_middle': {'x': 127, 'y': 73},\n",
       "    'right_eyebrow_upper_right_quarter': {'x': 135, 'y': 75}},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 45},\n",
       "    'smile': {'value': 0.041, 'threshold': 50.0}}}],\n",
       " 'image_id': 'xCWdLO/QxywBu3igdEIQRA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81314b6e",
   "metadata": {},
   "source": [
    "# 1.人脸分析 API "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "305fdd2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "payload = {\n",
    "    'api_key':'s6i96sHO1im2fnWJK0lFOjWyOiJvT1cK',\n",
    "    'api_secret':'1_Rm3dTYcF7iH3LzLVhywDZKasjLXW70',\n",
    "    'face_tokens':'987f1bd9e44e12763b3e9c6df387aae0',\n",
    "    'return_attributes':'gender,age,smiling,headpose'\n",
    "}\n",
    "\n",
    "r = requests.post(url=base_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e65683c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6ce71d0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 71,\n",
       " 'error_message': 'INVALID_FACE_TOKEN: 987f1bd9e44e12763b3e9c6df387aae0',\n",
       " 'request_id': '1647913712,e0f6b884-ae53-4307-91e6-a68b8ea80400'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6381a468",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "payload = {\n",
    "    'api_key':'s6i96sHO1im2fnWJK0lFOjWyOiJvT1cK',\n",
    "    'api_secret':'1_Rm3dTYcF7iH3LzLVhywDZKasjLXW70',\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7',\n",
    "}\n",
    "\n",
    "r = requests.post(url=base_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "02444f69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "27049607",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647913713,0b5f8eba-7784-45b3-a7f9-68eac7a20855',\n",
       " 'time_used': 573,\n",
       " 'faces': [{'face_token': 'b722025e46fde79c922becc40858951d',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4434c35e",
   "metadata": {},
   "source": [
    "# 2.人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5fecec78",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "payload = {\n",
    "    'api_key':'s6i96sHO1im2fnWJK0lFOjWyOiJvT1cK',\n",
    "    'api_secret':'1_Rm3dTYcF7iH3LzLVhywDZKasjLXW70',\n",
    "    'face_token1':'987f1bd9e44e12763b3e9c6df387aae0',\n",
    "    'face_token2':'581af6e2a5d3df809abfd13b2870cbee'\n",
    "}\n",
    "\n",
    "r = requests.post(url=base_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6436ab48",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8adfe198",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647913714,88ce8817-fbd7-4e1d-8cc5-7753e30be057',\n",
       " 'time_used': 70,\n",
       " 'error_message': \"INVALID_FACE_TOKEN: ['987f1bd9e44e12763b3e9c6df387aae0']\"}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f049e79e",
   "metadata": {},
   "source": [
    "# 3.封装应用函数 （内部使用的接口）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "152797dd",
   "metadata": {},
   "source": [
    "## 1. 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4f9f748f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "import requests\n",
    "Api_key = 's6i96sHO1im2fnWJK0lFOjWyOiJvT1cK'\n",
    "Api_sercret = '1_Rm3dTYcF7iH3LzLVhywDZKasjLXW70'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d130cd50",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_detect(Api_key,Api_secret,image_url,attributes=None,landmark=None):\n",
    "    \"\"\"face++ 人脸检测\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':Api_key,\n",
    "        'api_secret':Api_secret,\n",
    "        'image_url':image_url,\n",
    "        'return_attributes':attributes,\n",
    "        'return_landmark':landmark\n",
    "    }\n",
    "\n",
    "    r = requests.post(url=base_url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "395187ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647913716,6946552b-c4a3-40db-8a16-e10b7664bac9',\n",
       " 'time_used': 5,\n",
       " 'error_message': 'INVALID_IMAGE_URL'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 测试\n",
    "face_detect(Api_key,Api_sercret,'581af6e2a5d3df809abfd13b2870cbee')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b66250a9",
   "metadata": {},
   "source": [
    "# 作业：\n",
    "* 1. 尝试利用python函数的方法，封装 face++ 人脸检测，分析，对比\n",
    "* 2. 尝试调用face++ 人脸搜索API接口和人脸库API组\n",
    "* 3. 尝试设计宿舍人脸识别系统小应用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e873d2b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_detect(Api_key,Api_secret,face_tokens,attributes=None,landmark=None):\n",
    "    \"\"\"face++ 人脸分析\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "    payload = {\n",
    "        'api_key':Api_key,\n",
    "        'api_secret':Api_secret,\n",
    "        'face_tokens':face_tokens,\n",
    "        'return_attributes':attributes,\n",
    "        'return_landmark':landmark\n",
    "    }\n",
    "\n",
    "    r = requests.post(url=base_url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d350e7a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 37,\n",
       " 'error_message': 'VOID_REQUEST',\n",
       " 'request_id': '1647913717,d31d9db2-1356-4fb5-8fe5-f53921752c0e'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 测试\n",
    "face_detect(Api_key,Api_sercret,'581af6e2a5d3df809abfd13b2870cbee')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "118b4cc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 人脸库 API\n",
    "\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "    \n",
    "payload = {\n",
    "    'api_key':'s6i96sHO1im2fnWJK0lFOjWyOiJvT1cK',\n",
    "    'api_secret':'1_Rm3dTYcF7iH3LzLVhywDZKasjLXW70'\n",
    "    \n",
    "}\n",
    "  \n",
    "r = requests.post(url=base_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "80c1f08e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d85af772",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'fece19c643d19c1c94c2debd4e39e2c7',\n",
       " 'time_used': 158,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1647913717,6d82eb5c-7648-41a9-b31d-19107a229ffa',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fc0e6220",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 人脸搜索 API\n",
    "\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/search'\n",
    "    \n",
    "payload = {\n",
    "    'api_key':'s6i96sHO1im2fnWJK0lFOjWyOiJvT1cK',\n",
    "    'api_secret':'1_Rm3dTYcF7iH3LzLVhywDZKasjLXW70',\n",
    "    'face_token':'4f34fbc8af72fb08c9b9c81273580b25',\n",
    "    'faceset_token':'a400870ba301898d4e5ce1dca342904e'\n",
    "}\n",
    "  \n",
    "r = requests.post(url=base_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9b86707f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07d11735",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5821ae9d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
